/*
 * Copyright © 2022 XXXXXXXXX
 * @Author: zhangxiang
 * @Date: 2022-07-19 09:42:10
 * @LastEditors: zhangxiang
 * @LastEditTime: 2023-09-21 14:16:51
 * @Description:
 *
 */

import { getMenuNames } from '@/api/system-mgmt'
import Cookies from 'js-cookie'
import { getAllCloudVendor } from '@/api/cnap/hybrid-cloud'

const state = {
  sidebar: {
    opened: Cookies.get('sidebarStatus')
      ? !!+Cookies.get('sidebarStatus')
      : true, // 是否打开
    withoutAnimation: false
  },
  device: 'desktop', // 设备
  menuName: {},
  cloudVendors: [] // 云商资源池
}

const mutations = {
  /**
   * @description: 切换
   * @return {*}
   */
  TOGGLE_SIDEBAR: (state) => {
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
    if (state.sidebar.opened) {
      Cookies.set('sidebarStatus', 1)
    } else {
      Cookies.set('sidebarStatus', 0)
    }
  },
  /**
   * @description: 关闭
   * @return {*}
   */
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set('sidebarStatus', 0)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  /**
   * @description: 切换
   * @return {*}
   */
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  },
  /**
   * @description: 设置菜单名
   * @return {*}
   */
  SET_MENU_NAME: (state, menuName) => {
    state.menuName = menuName
  },
  /**
   * @description: 设置云商资源池
   * @return {*}
   */
  SET_CLOUD_VENDORS: (state, cloudVendors) => {
    state.cloudVendors = cloudVendors
  }
}

const actions = {
  /**
   * @description: 同步云商资源池
   * @param {*} commit
   * @param {*} state
   * @return {*}
   */
  syncCloudVendors({ commit, state }) {
    return new Promise((resolve, reject) => {
      // 调用接口
      getAllCloudVendor()
        .then((response) => {
          let { data } = response

          if (!(Array.isArray(data) && data.length > 0)) {
            // // 数据为空
            // return reject({
            //   errorMessage: '云商资源池为空'
            // })
            console.warn('云商资源池为空')
          }
          data = data.map(item => {
            return {
              ...item,
              label: item.cloudName + ': ' + item.accountName,
              value: item.cloudVendor + '$$' + item.accountName
            }
          })
          commit('SET_CLOUD_VENDORS', data) // 保存数据

          resolve(data) // 完成
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  /**
   * @description: 获取菜单名
   * @param {*} commit
   * @param {*} state
   * @return {*}
   */
  syncMenuNames({ commit, state }) {
    return new Promise((resolve, reject) => {
      // 调用接口
      getMenuNames()
        .then((response) => {
          const { data } = response

          if (!(Array.isArray(data) && data.length > 0)) {
            // 数据为空
            return reject('数据为空')
          }

          // 格式化数据
          const menuName = {}
          for (const item of data) {
            if (!Object.prototype.hasOwnProperty.call(menuName, item.menuKey)) {
              menuName[item.menuKey] = {
                menuOriginValue: item.menuOriginValue,
                menuValue: item.menuValue
              }
            }
          }
          commit('SET_MENU_NAME', menuName) // 存储

          resolve(data) // 完成
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  /**
   * @description: 设置云商资源池
   * @param {*} commit
   * @param {*} data
   * @return {*}
   */
  setCloudVendors({ commit }, data) {
    commit('SET_CLOUD_VENDORS', data)
  },
  /**
   * @description: 切换
   * @param {*} commit
   * @return {*}
   */
  toggleSideBar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  /**
   * @description: 关闭
   * @param {*} commit
   * @param {*} withoutAnimation
   * @return {*}
   */
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  /**
   * @description: 切换
   * @param {*} commit
   * @param {*} device
   * @return {*}
   */
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
